package com.wwy.config;

import com.alibaba.fastjson.JSONObject;
import com.wwy.*;
import com.wwy.domain.FiltrationTimeEntiry;
import com.wwy.domain.LabelEntity;
import com.wwy.domain.RssiEntity;
import com.wwy.domain.TypeEntity;
import com.wwy.service.MacDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020/8/13 16:29
 */
@Component
@Order(value = 1)
public class InitService implements ApplicationRunner {
    @Autowired
    private MqttSenderConfig mqttSenderConfig;
    @Autowired
    private MacDataService macDataService;
    @Value("${filePath}")
    private String filePath;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String typeFilePathName = filePath+"labelType.xy";
        String labelFilePathName = filePath+"label.xy";
        String rssiFilePathName = filePath+"rssi.xy";
        String filtrationTimeFilePathName = filePath+"filtrationTime.xy";
        String typeJson = Utils.getJson(typeFilePathName);
        String labelJson = Utils.getJson(labelFilePathName);
        String rssiJson = Utils.getJson(rssiFilePathName);
        String filtrationTimeJson = Utils.getJson(filtrationTimeFilePathName);
        if(typeJson != null && !"null".equals(typeJson)){
            List<TypeEntity> typeEntityList = new ArrayList<>();
            List<Map<String,Object>> typeEntities = JSONObject.parseObject(typeJson, List.class);
            for (Map<String, Object> typeEntity : typeEntities) {
                String typeName = String.valueOf(typeEntity.get("typeName"));
                String id = String.valueOf(typeEntity.get("id"));
                TypeEntity typeEntity1 = new TypeEntity();
                typeEntity1.setTypeName(typeName);
                typeEntity1.setId(id);
                typeEntityList.add(typeEntity1);
            }
            TypeDialog.typeEntityList = typeEntityList;
        }

        if(labelJson!=null && !"null".equals(labelJson)){
            List<LabelEntity> labelEntityList = new ArrayList<>();
            List<Map<String,Object>> labelEntities = JSONObject.parseObject(labelJson,List.class);
            for (Map<String, Object> labelEntity : labelEntities) {
                String lableId = String.valueOf(labelEntity.get("lableId"));
                String subScribeName = String.valueOf(labelEntity.get("subScribeName"));
                LabelEntity labelEntity1 = new LabelEntity();
                labelEntity1.setLableId(lableId);
                labelEntity1.setSubScribeName(subScribeName);
                labelEntityList.add(labelEntity1);
            }
            LabelDialog.labelEntityList = labelEntityList;
        }

        //获取主题
        List<LabelEntity> labelEntityList = LabelDialog.labelEntityList;
        String[] topics = new String[labelEntityList.size()];
        for (int i = 0; i < labelEntityList.size(); i++) {
            LabelEntity labelEntity = labelEntityList.get(i);
            String subScribeName = labelEntity.getSubScribeName();
            topics[i] = subScribeName;
        }

        mqttSenderConfig.addLisTenTopic(topics);
        //添加面板
        SwingArea instance = SwingArea.getInstance();
        for (TypeEntity typeEntity : TypeDialog.typeEntityList) {
            instance.creatLabelTable(typeEntity);
        }

        //获取阀值
        if(rssiJson !=null && !"null".equals(rssiJson)){
            Map map = JSONObject.parseObject(rssiJson, Map.class);
            RssiEntity rssiEntity1 = new RssiEntity();
            int start = Integer.parseInt(String.valueOf(map.get("start")));
            int end = Integer.parseInt(String.valueOf(map.get("end")));
            rssiEntity1.setStart(start);
            rssiEntity1.setEnd(end);
            RssiDialog.rssiEntity = rssiEntity1;
        }
        //时间范围
        if(filtrationTimeJson !=null && !"null".equals(filtrationTimeJson)){
            Map map = JSONObject.parseObject(filtrationTimeJson, Map.class);
            FiltrationTimeEntiry filtrationTimeEntiry = new FiltrationTimeEntiry();
            int time = Integer.parseInt(String.valueOf(map.get("time")));
            filtrationTimeEntiry.setTime(time);
            FiltrationTimeDialog.filtrationTimeEntiry = filtrationTimeEntiry;
        }
        //初始化时间段筛选
        new Thread(()->{
            System.out.println("初始化时间段筛选开始");
            macDataService.timerData();
        }).start();





    }
}
